package controller;
import model.*;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 更新管理员信息
 * Created by yvdedu.com on 2016/9/27.
 */
@WebServlet(urlPatterns = "/update_manage")
public class update_manage extends HttpServlet{
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setCharacterEncoding("utf-8");
        req.setCharacterEncoding("utf-8");
        String name=req.getParameter("name");
        String old_pwd=req.getParameter("old_pwd");
        String new_pwd=req.getParameter("new_pwd");
        String sec_pwd=req.getParameter("sec_pwd");
        if(name!=null&&old_pwd!=null&&new_pwd!=null&&sec_pwd!=null){
            MySql mySql=new MySql();
            model.Manage manage=mySql.getManageByName(name);
            if(manage==null){
                resp.getWriter().print("账号不存在，请重试！");
            }else{
                if(!old_pwd.equals(manage.getPwd())){
                    resp.getWriter().print("原密码错误,请重试！");
                }else if(!new_pwd.equals(sec_pwd)){
                    resp.getWriter().print("两次密码不一致,请重试！");
                }else{
                    Boolean result=mySql.setUserPwd(name,new_pwd);
                    if (result){
                        resp.getWriter().print("恭喜您,修改成功！");
                    }else{
                        resp.getWriter().print("对不起,密码修改失败,请重试！");
                    }
                }
            }
        }else{
            resp.getWriter().print("账号或密码不能为空！");
        }
    }
}
